const app = getApp()

Page({
  data: {
    //判断小程序的API，回调，参数，组件等是否在当前版本可用。
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  onLoad: function () {
    var that = this;
    // 查看是否授权
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              app.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }

              wx.request({
                //获取openid接口
                url: 'https://tally.slickghost.com/demo2/Access/OpenId',
                data: app.globalData.data,
                method: 'get',
                success: function (res) {
                  // console.log(res)
                  console.log(res.data.constructor)
                  if (res.data.constructor === String)
                  {
                    app.globalData.openid = res.data
                    wx.request({
                      url: 'https://tally.slickghost.com/demo2/User/saveUser',
                      data: {
                        nickName: app.globalData.userInfo.nickName,
                        openID: app.globalData.openid,
                        gender: app.globalData.userInfo.gender,
                        Province: app.globalData.userInfo.province,
                        city: app.globalData.userInfo.city,
                        avatarUrl: app.globalData.userInfo.avatarUrl,
                      },
                      method: 'get',
                      success: function (res) {
                        wx.request({
                          url: 'https://tally.slickghost.com/demo2/Answer/selectAnswer',
                          data: {
                            openID: app.globalData.openid
                          },
                          method: 'get',
                          success: function (res) {
                            if (res.data.length !== 0) {
                              app.globalData.correct = res.data[0].correctCount;
                              app.globalData.wrong = res.data[0].errorCount;
                              app.globalData.score = res.data[0].score;
                            } else {
                              app.globalData.correct = 0;
                              app.globalData.wrong = 0;
                              app.globalData.score = 0;
                            }
                            wx.switchTab({
                              url: '../indexs/indexs'
                            })
                          }

                        });
                      }

                    });
                  }

                }
              })
            }
          })
        }
      }
    })

  },
  bindGetUserInfo: function (e) {
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      var that = this;
      //插入登录的用户的相关信息到数据库
      wx.getUserInfo({
        success: res => {
          // 可以将 res 发送给后台解码出 unionId
          app.globalData.userInfo = res.userInfo

          // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
          // 所以此处加入 callback 以防止这种情况
          if (this.userInfoReadyCallback) {
            this.userInfoReadyCallback(res)
          }

          wx.request({
            //获取openid接口
            url: 'https://tally.slickghost.com/demo2/Access/OpenId',
            data: app.globalData.data,
            method: 'get',
            success: function (res) {
              // console.log(res)
              //console.log(res.data.data.openid)
              if (res.data.constructor === String) {
                app.globalData.openid = res.data
                wx.request({
                  url: 'https://tally.slickghost.com/demo2/User/saveUser',
                  data: {
                    nickName: app.globalData.userInfo.nickName,
                    openID: app.globalData.openid,
                    gender: app.globalData.userInfo.gender,
                    Province: app.globalData.userInfo.province,
                    city: app.globalData.userInfo.city,
                    avatarUrl: app.globalData.userInfo.avatarUrl,
                  },
                  method: 'get',
                  success: function (res) {
                    wx.request({
                      url: 'https://tally.slickghost.com/demo2/Answer/selectAnswer',
                      data: {
                        openID: app.globalData.openid
                      },
                      method: 'get',
                      success: function (res) {
                        if (res.data.length !== 0) {
                          app.globalData.correct = res.data[0].correctCount;
                          app.globalData.wrong = res.data[0].errorCount;
                          app.globalData.score = res.data[0].score;
                        } else {
                          app.globalData.correct = 0;
                          app.globalData.wrong = 0;
                          app.globalData.score = 0;
                        }
                        wx.switchTab({
                          url: '../indexs/indexs'
                        })
                      }

                    });
                  }

                });
              }

            }
          })
        }
      })
    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '警告',
        content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
        showCancel: false,
        confirmText: '返回授权',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击了“返回授权”')
          }
        }
      })
    }
  },


})
